<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\Repositories\PostsRepositores;
use App\Http\Requests\Api\PostsRequests\CircleRequest;
use App\Lib\JwtAuth;
use App\Lib\Uploads\UploadHandler;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\WxSlideshow;
use Illuminate\Auth\Events\Validated;
use Illuminate\Http\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Validator;


class IndexController extends BaseController
{
    public function test(CircleRequest $request)
    {
//        $jwtAuth = JwtAuth::getToken('uid',61);
//        $a=JwtAuth::getUserId($jwtAuth,'uid');
//        return $a;
        //数据验证
//        $request->validate('addMsg');
//        $request->validate('deleteMsg');
        $postImage =
            [[
                'posts_id' => 1,
                'user_id' => 1,
                'img_url' => 1
            ], [
                'posts_id' => 1,
                'user_id' => 1,
                'img_url' => 1
            ]];
//        dump($postImage);
        $postImg = new WxPostsImg();
        $postImg->addAll($postImage);

    }

    /**
     * 首页轮播图
     */
    public function banner()
    {
        $banner = WxSlideshow::where('slideshow_state', 1)->where('slideshow_type', '<>', 3)->orderBy('sort', 'asc')->get();
        return $this->success($banner);
    }

    /**
     * 热门文章
     * @return string
     */
    public function choiceness()
    {
        return $this->success(PostsRepositores::choiceness());
    }


    public function getPosts(Request $request)
    {
        $token = $request->header('token');
        $type = $request->type;
        $plateId = $request->plate_id;

        $uid = '';
        if ($token) {
//           return $this->success($request->header('token'));
            $uid = JwtAuth::getUserId($token, 'uid');

        }

        $limit = $request->input('limit', 6);
        // 我关注的帖子列表
        if ($type == 0) {
            $data = PostsRepositores::followPosts($uid, $limit);
        }

        // 推荐的帖子列表
        if ($type == 1) {
            $data = PostsRepositores::list($uid, $limit);
        }


        // 热榜的帖子列表
        if ($type == 2) {
            $data = PostsRepositores::hotPosts($uid, $limit);
        }


        // 通过板块查找帖子列表
        if ($type >= 3) {
            $data = PostsRepositores::getListByPlate($uid, $plateId, $limit);
        }

        return $this->success($data);
    }
}
